﻿using System;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.HTML.Components;

namespace ASPNETVN.PORTAL.Modules.HTML.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.Delete += AddEditItem_Delete;
            this.Update += AddEditItem_Update;

            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;

            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            if (this.ItemID == 0)
            {
                Item.InsertItem(this.ModuleID, Server.HtmlEncode(txtHtmlContent.Value));
            }
            else
            {
                Item.UpdateItem(this.ItemID, this.ModuleID, Server.HtmlEncode(txtHtmlContent.Value));
            }
            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Item item = Item.Load(this.ItemID);
                if (item != null)
                {
                    txtHtmlContent.Value = Server.HtmlDecode(item.HtmlContent);
                }
            }
        }
    }
}